/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.mach.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.mach.entity.StandardParts;
import org.springblade.mach.excel.StandardPartsExcel;
import org.springblade.mach.vo.StandardPartsVO;
import org.springblade.mach.mapper.StandardPartsMapper;
import org.springblade.mach.service.IStandardPartsService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.rowset.serial.SerialException;
import java.sql.Wrapper;
import java.util.List;
import java.util.Objects;

/**
 * 标准件库表 服务实现类
 *
 * @author zhp
 * @since 2020-12-30
 */
@Service
public class StandardPartsServiceImpl extends BaseServiceImpl<StandardPartsMapper, StandardParts> implements IStandardPartsService {

	@Override
	public IPage<StandardPartsVO> selectStandardPartsPage(IPage<StandardPartsVO> page, StandardPartsVO standardParts) {
		return page.setRecords(baseMapper.selectStandardPartsPage(page, standardParts));
	}

	@Override
	public Integer getStandardPartsByCode(String code, String name) {
		return baseMapper.getStandardPartsByCode(code, name);
	}

	@Override
	public boolean saveCheck(StandardParts standardParts) throws SerialException {
		String code = standardParts.getCode();
		String name = standardParts.getName();
		Integer standardPartsByCode = this.getStandardPartsByCode(code, name);
		if (standardPartsByCode!=null && standardPartsByCode>0) {
           throw new SerialException(StringUtil.format("该标准件代号已经存在"));
		}
		return saveOrUpdate(standardParts);
	}

	@Override
	public StandardParts getOneByPartsCode(String code, String name) {

		return baseMapper.getOneByPartsCode(code, name);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void importStandardParts(List<StandardPartsExcel> standardPartsExcelList, Boolean isCovered) {
		standardPartsExcelList.forEach(standardPartsExcel -> {
			StandardParts parts = Objects.requireNonNull(BeanUtil.copy(standardPartsExcel, StandardParts.class));
			// 是否覆盖
			if (isCovered) {
				StandardParts oldStandardParts = baseMapper.getOneByPartsCode(standardPartsExcel.getCode(),standardPartsExcel.getName());
				if (oldStandardParts != null && oldStandardParts.getId()!= null) {
					parts.setId(oldStandardParts.getId());
					saveOrUpdate(parts);
					return;
				}
			}
			// 保存信息
			save(parts);
		});

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateStandardParts(StandardParts standardParts) {
		Integer standardPartCount = baseMapper.selectCount(Wrappers.<StandardParts>query().lambda()
			.eq(StandardParts::getCode, standardParts.getCode())
			.notIn(StandardParts::getId, standardParts.getId()));
        if (standardPartCount >0) {
        	throw new ServiceException(StringUtil.format("当前编号 [{}] 已存在!", standardParts.getCode()));
		}
		return saveOrUpdate(standardParts);
	}

}
